//
//  PageHeaderCollectionView.m
//  AFNetworking
//
//  Created by admin on 2020/4/29.
//

#import "PageHeaderCollectionView.h"

@implementation PageHeaderCollectionCellData
@end


@implementation PageHeaderCollectionCell

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self confitUI];
    }
    return self;
}

-(void)confitUI{
    self.backgroundColor = UIColor.whiteColor;
    [self addSubview:self.titleLab];
    [self addSubview:self.remarkView];
}
-(UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
    }
    return _titleLab;
}
-(UIView *)remarkView{
    if (!_remarkView) {
        _remarkView = [UIView new];
        [_remarkView ug_radius:1.5];
    }
    return _remarkView;
}
-(void)setSelected:(BOOL)selected{
    [super setSelected:selected];
    
    if (selected) {
        _remarkView.backgroundColor = [UIColor ug_hexString:@"#007AFF"];
        _titleLab.textColor = COLOR_33;
         _titleLab.font = FONT_BSYS16;
        
    }else{
        _remarkView.backgroundColor = UIColor.clearColor;
        _titleLab.textColor = COLOR_66;
        _titleLab.font = FONT_SYS15;
       
    }
}
-(void)reload:(id)item{
    if ([item isKindOfClass:[PageHeaderCollectionCellData class]]) {
        PageHeaderCollectionCellData *data = (PageHeaderCollectionCellData*)item;
        _titleLab.text = data.title;
    }else{

        _titleLab.text = [NSString stringWithFormat:@"%@",item];
    }
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self);
    }];
    [_remarkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self);
        make.height.mas_equalTo(3);
        make.width.mas_equalTo(KAutoAcale(20));
        make.centerX.mas_equalTo(self);
        
    }];
}

@end

@implementation PageHeaderCollectionView

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    
    if (self == [super initWithFrame:frame collectionViewLayout:layout]) {
        self.delegate = self;
        self.dataSource = self;
        self.identifier =@"PageHeaderCollectionCell";
        
    }
    return self;
}
-(void)setIdentifier:(NSString *)identifier{
    _identifier = identifier;
    [self registerClass:NSClassFromString(_identifier) forCellWithReuseIdentifier:_identifier];
}
-(void)setItems:(NSArray *)items{
    _items = items;
    _selectItem = _items.firstObject;
    [self reloadData];
}

#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _items.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    Class cls = NSClassFromString(_identifier);
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:_identifier forIndexPath:indexPath];
    id data = [_items objectAtIndex:indexPath.row];
   
    [cell setSelected:(data == _selectItem)];
 
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    SEL sel = NSSelectorFromString(@"reload:");
    [cell performSelector:sel withObject:data];
#pragma clang diagnostic pop
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    id data = [_items objectAtIndex:indexPath.row];
    _selectItem = data;
    [collectionView reloadData];
    if (_cellClick) {
        _cellClick(data);
    }
}

@end
